<?php

class status_TimelineController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }

  //public timeline
  function publicAction() {
  	
  	$beforeStatusId = $this->_getParam('beforeStatusId');
	$afterStatusId = $this->_getParam('afterStatusId');

		//We select the stream per page if $beforeStatusId is empty and $afterStatusId 
		if(empty($beforeStatusId ) && empty($afterStatusId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
		} elseif(!empty($beforeStatusId )) {
			$searchMode = 'beforeStatusId';
		} // end beforestatus is not empty
		elseif(!empty($afterStatusId )) {
			$searchMode = 'afterStatusId';			
		} // end beforestatus is not empty		
  	
    require_once('status/objects/status.php');
    try{
      //as it is public timeline, wa can go directly in the status table to get them
	  $statuses = Status_Object_Status::listing($this->_getParam('perPage'),$this->_getParam('page'),$beforeStatusId,$afterStatusId,$searchMode);
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	$data['status'] = 	'OK';
	
	//we can t use status as it is alredy used to say evrything went fine
	$data['perPage'] = Status_Object_Status::formatPerPage($this->_getParam('perPage'));
	if('page' == $searchMode) $data['page'] = Status_Object_Status::formatPage($this->_getParam('page'));
	if('beforeStatusId' == $searchMode) $data['queryStatusId'] = $beforeStatusId;
	if('afterStatusId' == $searchMode) $data['queryStatusId'] = $afterStatusId;
		
	$data['data'] =  $statuses;	
	
	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;
  } //end create action
  
  //Home timeline of the user
  function homeAction() {
    require_once('status/objects/timeline.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$userId = (int) $this->_getParam('userId');
    	if(empty($userId)) $userId = Zend_Registry::get('app_user')->getId();
		
		//We collect the different search parameters
		$beforeStatusId = $this->_getParam('beforeStatusId');
		$afterStatusId = $this->_getParam('afterStatusId');
		
    	$timeline = new Status_Object_Timeline('home',$userId);
    	
		//Statuses search depends on the mode. If the mode is not empty, we search based on the status id
		//if empty we search base on the page
		
		//We select the stream per page if $beforeStatusId is empty and $afterStatusId 
		if(empty($beforeStatusId ) && empty($afterStatusId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$statuses = $timeline->listingStreamPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeStatusId )) {
			$searchMode = 'beforeStatusId';
			$statuses = $timeline->listingStreamPerStatusId('before',$beforeStatusId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterStatusId )) {
			$searchMode = 'afterStatusId';
			$statuses = $timeline->listingStreamPerStatusId('after',$afterStatusId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty
		
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    
    try{
		$oUser = new Myzen_User(array('id' => $userId));
	} catch(Zend_Auth_Exception $e) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 	$e->getCode();
		$data['error']['message'] = 	$e->getMessage();
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;    		
	}    
    
    $data['status']= 'OK';
    $data['user']= $oUser->asArray();
    $data['follow'] = Status_Object_Timeline::getConnexionNumbers($userId);

	$data['mode'] = $searchMode;
	$data['perPage'] = Status_Object_Timeline::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Status_Object_Timeline::formatPage($this->_getParam('page'));
	if('beforeStatusId' == $searchMode) $data['queryStatusId'] = $beforeStatusId;
	if('afterStatusId' == $searchMode) $data['queryStatusId'] = $afterStatusId;
	
	$data['data'] =  $statuses;    
    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end 
  
  
  //USer timeline (what the user published)
  function userAction() {
    require_once('status/objects/timeline.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$userId = $this->_getParam('userId');
    	$userPseudo = $this->_getParam('userPseudo');
    	//If user id is empty and pseudo is also empty we take the authenticated user id 
    	if(empty($userId) && empty($userPseudo)) {
    		$oUser = Zend_Registry::get('app_user');
    		$userId = $oUser->getId();
    	}
    	//If the search is using the pseudo, we need to retrieve the identifier of the user
    	if(empty($userId) && !empty($userPseudo)) {
    		try{
				$oUser = new Myzen_User(array('pseudo' => $userPseudo));
				$userId = $oUser->getId();
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}	
    	}

		//We collect the different search parameters
		$beforeStatusId = $this->_getParam('beforeStatusId');
		$afterStatusId = $this->_getParam('afterStatusId');		

	
    	//we get the timeline of the user
    	$timeline = new Status_Object_Timeline('user',$userId);
		
		//We select the stream per page if $beforeStatusId is empty and $afterStatusId 
		if(empty($beforeStatusId ) && empty($afterStatusId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$statuses = $timeline->listingStreamPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeStatusId )) {
			$searchMode = 'beforeStatusId';
			$statuses = $timeline->listingStreamPerStatusId('before',$beforeStatusId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterStatusId )) {
			$searchMode = 'afterStatusId';
			$statuses = $timeline->listingStreamPerStatusId('after',$afterStatusId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty


	} catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    
    //now we may need to retrieve the user object if we are in the case where the app provided userId
    	if(!isset($oUser)) {
     		try{
				$oUser = new Myzen_User(array('id' => $userId));
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}   		
    	}
    
    //and we need to get the relationship between the 2 people
    $isFollowing = Status_Object_Timeline::isFollowing(Zend_Registry::get('app_user'), $oUser);
    
    
    //now we can create the message
    $data['status']= 'OK';
    $data['user']= $oUser->asArray();
    $data['follow'] = Status_Object_Timeline::getConnexionNumbers($oUser->getId());
    $data['isFollowing'] = $isFollowing;
  	

	$data['mode'] = $searchMode;
	$data['perPage'] = Status_Object_Timeline::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Status_Object_Timeline::formatPage($this->_getParam('page'));
	if('beforeStatusId' == $searchMode) $data['queryStatusId'] = $beforeStatusId;
	if('afterStatusId' == $searchMode) $data['queryStatusId'] = $afterStatusId;
	
	$data['data'] =  $statuses; 	
	
	$data['data'] =  $statuses;    
    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end  user




  //Mention timeline of the user
  function mentionAction() {
    require_once('status/objects/timeline.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$userId = $this->_getParam('userId');
    	$userPseudo = $this->_getParam('userPseudo');
    	//If user id is empty and pseudo is also empty we take the authenticated user id 
    	if(empty($userId) && empty($userPseudo)) {
    		$oUser = Zend_Registry::get('app_user');
    		$userId = $oUser->getId();
    	}
    	   	
    	//If the search is using the pseudo, we need to retrieve the identifier of the user
    	if(empty($userId) && !empty($userPseudo)) {
    		try{
				$oUser = new Myzen_User(array('pseudo' => $userPseudo));
				$userId = $oUser->getId();
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}	
    	}
    	
 		//We collect the different search parameters
		$beforeStatusId = $this->_getParam('beforeStatusId');
		$afterStatusId = $this->_getParam('afterStatusId');	

	//we get the timeline of the user
    	$timeline = new Status_Object_Timeline('mention',$userId);


		//We select the stream per page if $beforeStatusId is empty and $afterStatusId 
		if(empty($beforeStatusId ) && empty($afterStatusId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$statuses = $timeline->listingStreamPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeStatusId )) {
			$searchMode = 'beforeStatusId';
			$statuses = $timeline->listingStreamPerStatusId('before',$beforeStatusId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterStatusId )) {
			$searchMode = 'afterStatusId';
			$statuses = $timeline->listingStreamPerStatusId('after',$afterStatusId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty



		} catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    
    //now we may need to retrieve the user object if we are in the case where the app provided userId
    	if(!isset($oUser)) {
     		try{
				$oUser = new Myzen_User(array('id' => $userId));
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}   		
    	}
    
    //and we need to get the relationship between the 2 people
    $isFollowing = Status_Object_Timeline::isFollowing(Zend_Registry::get('app_user'), $oUser);
    
    
    //now we can create the message
    $data['status']= 'OK';
    $data['user']= $oUser->asArray();
    $data['follow'] = Status_Object_Timeline::getConnexionNumbers($oUser->getId());
    $data['isFollowing'] = $isFollowing;
	
	
	$data['mode'] = $searchMode;
	$data['perPage'] = Status_Object_Timeline::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Status_Object_Timeline::formatPage($this->_getParam('page'));
	if('beforeStatusId' == $searchMode) $data['queryStatusId'] = $beforeStatusId;
	if('afterStatusId' == $searchMode) $data['queryStatusId'] = $afterStatusId;
	
	
	$data['data'] =  $statuses;    
    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end


  
  
  
  
  
  
  
  
  
  
  
  
  
    //Tag timeline
  function tagAction() {
    require_once('status/objects/timeline.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$tagId = $this->_getParam('tagId');
    	if(empty($tagId)) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	72;
			$data['error']['message'] = 'Tag is mandatory';
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;			
		}
		
		//We collect the different search parameters
		$beforeStatusId = $this->_getParam('beforeStatusId');
		$afterStatusId = $this->_getParam('afterStatusId');
		
    	$timeline = new Status_Object_Timeline('tag',$tagId);

		//Statuses search depends on the mode. If the mode is not empty, we search based on the status id
		//if empty we search base on the page
		
		//We select the stream per page if $beforeStatusId is empty and $afterStatusId 
		if(empty($beforeStatusId ) && empty($afterStatusId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$statuses = $timeline->listingStreamPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeStatusId )) {
			$searchMode = 'beforeStatusId';
			$statuses = $timeline->listingStreamPerStatusId('before',$beforeStatusId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterStatusId )) {
			$searchMode = 'afterStatusId';
			$statuses = $timeline->listingStreamPerStatusId('after',$afterStatusId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty
		
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    
    try{
		$oUser = new Myzen_User(array('id' => $userId));
	} catch(Zend_Auth_Exception $e) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 	$e->getCode();
		$data['error']['message'] = 	$e->getMessage();
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;    		
	}    
    
    $data['status']= 'OK';
    $data['user']= $oUser->asArray();
    $data['follow'] = Status_Object_Timeline::getConnexionNumbers($oUser->getId());

	$data['mode'] = $searchMode;
	$data['perPage'] = Status_Object_Timeline::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Status_Object_Timeline::formatPage($this->_getParam('page'));
	if('beforeStatusId' == $searchMode) $data['queryStatusId'] = $beforeStatusId;
	if('afterStatusId' == $searchMode) $data['queryStatusId'] = $afterStatusId;
	
	$data['data'] =  $statuses;    
    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end
  
  
  //allow another backend to subscribe to a particular tag
  function subscribeAction() {
  		require_once('status/objects/listener.php');
  		try{
  			Status_Object_Listener::addListener($this->_getParam('listenerModule'),$this->_getParam('listenerController'),$this->_getParam('listenerAction'),$this->_getParam('listenerTag'));	
  		}catch(Status_Object_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;   			
  		}
  	    $data['status']= 'OK';
	 	echo Zend_Registry::get('dt_serializer')->serialize($data);
	    return;  	    
  	
  } //end subscribeAction
  
  
  
  
} //End class


?>